import 'package:flutter/foundation.dart';
import '../models/user.dart';
import '../services/user_service.dart';

class UserProvider extends ChangeNotifier {
  final UserService _userService = UserService();
  User? _currentUser;
  List<User> _users = [];
  bool _isLoading = false;
  String? _error;

  User? get currentUser => _currentUser;
  List<User> get users => _users;
  bool get isLoading => _isLoading;
  String? get error => _error;

  // 初始化
  Future<void> init() async {
    await loadUsers();
    await loadCurrentUser();
  }

  // 加载所有用户
  Future<void> loadUsers() async {
    _isLoading = true;
    _error = null;
    notifyListeners();

    try {
      _users = await _userService.getAllUsers();
    } catch (e) {
      _error = e.toString();
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  // 加载当前用户
  Future<void> loadCurrentUser() async {
    _isLoading = true;
    _error = null;
    notifyListeners();

    try {
      _currentUser = await _userService.getCurrentUser();
    } catch (e) {
      _error = e.toString();
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  // 用户登录
  Future<bool> login(String username, String password) async {
    _isLoading = true;
    _error = null;
    notifyListeners();

    try {
      _currentUser = await _userService.login(username, password);
      return true;
    } catch (e) {
      _error = e.toString();
      return false;
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  // 用户注销
  Future<void> logout() async {
    _isLoading = true;
    _error = null;
    notifyListeners();

    try {
      await _userService.logout();
      _currentUser = null;
    } catch (e) {
      _error = e.toString();
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  // 更新用户信息
  Future<bool> updateUser(User user) async {
    _isLoading = true;
    _error = null;
    notifyListeners();

    try {
      final updatedUser = await _userService.updateUser(user);
      
      // 如果更新的是当前用户，则更新当前用户信息
      if (_currentUser != null && _currentUser!.id == updatedUser.id) {
        _currentUser = updatedUser;
        await _userService.saveCurrentUser(updatedUser);
      }
      
      // 更新用户列表
      await loadUsers();
      
      return true;
    } catch (e) {
      _error = e.toString();
      return false;
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  // 关注/取消关注用户
  Future<bool> toggleFollow(String userId) async {
    if (_currentUser == null) {
      _error = 'No user logged in';
      notifyListeners();
      return false;
    }

    _isLoading = true;
    _error = null;
    notifyListeners();

    try {
      final List<String> updatedFollow = List.from(_currentUser!.follow);
      
      if (updatedFollow.contains(userId)) {
        updatedFollow.remove(userId);
      } else {
        updatedFollow.add(userId);
      }
      
      final updatedUser = _currentUser!.copyWith(follow: updatedFollow);
      await updateUser(updatedUser);
      
      return true;
    } catch (e) {
      _error = e.toString();
      return false;
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  // 用户注册
  Future<bool> register({
    required String username,
    required String password,
    required String nickname,
    String? signature,
  }) async {
    _isLoading = true;
    _error = null;
    notifyListeners();

    try {
      await _userService.register(
        username: username,
        password: password,
        nickname: nickname,
        signature: signature,
      );
      
      // 更新用户列表
      await loadUsers();
      
      return true;
    } catch (e) {
      _error = e.toString();
      return false;
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  // 清除错误
  void clearError() {
    _error = null;
    notifyListeners();
  }
} 